package com.qiqv.service.impl;

import com.qiqv.config.SongData;
import com.qiqv.dto.Song;
import com.qiqv.service.SongService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 歌曲类Service操作类
 * PS： 所有数据暂时不进行数据库的持久化操作，只保存在项目中
 * @author hjr
 * @version 1.0
 * @date 2023/2/14 12:45
 */
@Service
public class SongServiceImpl implements SongService {
    
    @Autowired
    private SongData songData;

    /**
     * 根据歌名获取歌曲信息
     * @param songName  歌名
     * @return
     */
    @Override
    public Song findSongByName(String songName) {
        if(StringUtils.isBlank(songName)){
            return null;
        }
        List<Song> songList = songData.getSongList();
        Song hitSong = songList.stream().map(o -> {
            String note = o.getNote();
            if(StringUtils.isNotBlank(note)){
                List<String> singleNotes = Arrays.asList(note.split(" "));
                o.setSingleNoteList(singleNotes);
            }
            return o;
        }).filter(o -> songName.equals(o.getName())).findFirst().orElse(null);
        return hitSong;
    }

    @Override
    public List<Song> getAllSongs() {
        return songData.getSongList();
    }
}
